package cn.maihe.elg.operation.supports.electronicseal.kt;

import com.google.common.collect.Lists;
import com.kinsec.KTConvert;
import com.kinsec.KTSignature;
import com.kinsec.SignatureInfo;
import com.kinsec.dom.Attachment;
import com.kinsec.utils.OESException;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;


public class KTAutoSealJavaOfd {
    static final String baseURL = "125.77.73.161:22288";
//    static final String baseURL = "125.77.73.161:2040";
//    static final String sealID = "test";
    static final String sealID = "35010000008558";
    static final String password = "12345678";
//    static final String password = "111111";
    static KTConvert ktConvert = new KTConvert(baseURL);
    static KTSignature ktSignature = new KTSignature(baseURL);
    static Path basePath  = Paths.get("d:/tm");


//    @Before
//    public void init(){
//        try {
//            basePath = ResourceUtils.getFile("classpath:").toPath();
//            System.out.println("baseURL = " + baseURL);
//        } catch (FileNotFoundException e) {
//           throw new RuntimeException(e);
//        }
//    }

    @Test
    public void  pdfSealTest(){

        Path srcPath = basePath.resolve("到账证明模板.pdf");
        Path targetPath = srcPath.resolveSibling("sealed_到账证明模板.pdf");
        int pageNum = 0;
        float x = 150;
        float y = 180;

//        KTAutoSealJavaClt clt = new KTAutoSealJavaClt( "125.77.73.161:2040", "test", "11111", true );


    }

    @Test
    public void singleSealTest() throws FileNotFoundException, IOException {

    	//目前仅支持ofd文件进行签章
        Path srcPath = basePath.resolve("到账证明.ofd");
        Path targetPath = srcPath.resolveSibling("sealed_到账证明.ofd");
        int pageNum = 0;
        float x = 150;
        float y = 180;

//        boolean nRet = ktSignature.kt_ofdStamp(srcPath.toString(), pageNum, sealID, password, x, y, Files.newOutputStream(targetPath));
//        if (!nRet) {
//            System.out.println("singleSeal false");
//            return;
//        }

        System.out.println("singleSeal success!!!");
    }

    @Test
    public  void unionSealTest() throws IOException {
		//目前仅支持ofd文件进行签章

		Path srcPath = basePath.resolve("担保保函.ofd");
		Path targetPath = srcPath.resolveSibling("sealed_担保保函.ofd");

        ArrayList<SignatureInfo> signatureInfos = new ArrayList<SignatureInfo>();
        String sealID1 = "35010200014143";
        String sealID2 = "35010300014144";
        SignatureInfo signatureInfo = new SignatureInfo(0, sealID1, "12345678", 120, 180);
        SignatureInfo signatureInfo2 = new SignatureInfo(0, sealID2, "12345678", 80, 190);

        signatureInfos.add(signatureInfo);
        signatureInfos.add(signatureInfo2);

//        boolean union = ktSignature.kt_ofdStamps(srcPath.toString(), signatureInfos, Files.newOutputStream(targetPath));
//        if (!union) {
//            System.out.println("seal false");
//            return;
//        }

        System.out.println("success!!!");
    }

    @Test
    public void ConvertTest() throws FileNotFoundException, IOException, OESException {
        //源转换文件 支持pdf doc docx html xls
		Path srcPdfFile = basePath.resolve("担保保函.pdf");
        ktConvert.kt_officeToOFD(srcPdfFile.toString(), Files.newOutputStream(basePath.resolve("担保保函.ofd")));
        System.out.println("convert success!!!");

        //源转换文件 支持pdf doc docx html xls
        Path srcPdfFile1 = basePath.resolve("到账证明.pdf");
        ktConvert.kt_officeToOFD(srcPdfFile1.toString(), Files.newOutputStream(basePath.resolve("到账证明.ofd")));
        System.out.println("convert success!!!");

//		Path srcDocFile = basePath.resolve("a.docx");
//		ktConvert.kt_officeToOFD(srcDocFile.toString(), Files.newOutputStream(basePath.resolve("a.docx.ofd")));
//		System.out.println("convert success!!!");

	}

	@Test
    public  void ConvertAddAttachment() throws FileNotFoundException, IOException, OESException {
        try {
        	//带附件的转换，目前源文件只支持pdf ofd格式
			Path srcPath = basePath.resolve("担保保函.ofd");
			Path targetPath = basePath.resolve("担保保函attach.ofd");
			Path attachPath = basePath.resolve("到账证明.ofd");

            ktConvert.kt_addAttachment(srcPath.toFile(), Files.newOutputStream(targetPath), "到账证明", "ofd", Files.newInputStream(attachPath));
            System.out.println("convert success!!!");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void ConvertAddAttachments() throws FileNotFoundException, IOException, OESException {

		//带附件的转换，目前源文件只支持pdf ofd格式

        Path applyFile = Paths.get("E:\\Workspaces\\ideawork\\maihe\\elg\\sanming\\fzdx-web-ningde\\fzdx-manage-api-ningde\\src\\main\\resources\\ofd\\C00061451444672820465665\\电子保函申请书.docx");
        Path qualificationPath = Paths.get("E:\\Workspaces\\ideawork\\maihe\\elg\\sanming\\fzdx-web-ningde\\fzdx-manage-api-ningde\\src\\main\\resources\\upload\\image\\regist\\d726efda-51fd-453a-99ef-6bc5474ad377.jpg");
        Path licensePath = Paths.get("E:\\Workspaces\\ideawork\\maihe\\elg\\sanming\\fzdx-web-ningde\\fzdx-manage-api-ningde\\src\\main\\resources\\upload\\image\\regist\\5cb8f57b-d372-4dac-90c2-b781c2043c9f.jpg");
Path receiveOfd = applyFile.resolveSibling("到账证明.ofd");
Path ofdStructXmlPath = applyFile.resolveSibling("结构化数据文件.xml");
        Attachment guaranteeOfdAtt = new Attachment("电子保单（合同）原文件", "ofd", Files.newInputStream(applyFile));
        Attachment guaranteeOfdAttPdf = new Attachment("电子保单（合同）PDF文件", "ofd", Files.newInputStream(applyFile));
        Attachment applyFileAtt = new Attachment("电子保函申请单原文件", "ofd", Files.newInputStream(applyFile));
        Attachment applyFileAttPdf = new Attachment("电子保函申请单PDF文件", "ofd", Files.newInputStream(applyFile));

        Attachment qualificationAtt = new Attachment("企业资质文件", "jpeg", Files.newInputStream(qualificationPath));
        Attachment licenseAtt = new Attachment("企业营业执照", "jpeg", Files.newInputStream(licensePath));
        Attachment receiveOfdAtt = new Attachment("到账证明", "pdf", Files.newInputStream(receiveOfd));
        Attachment ofdStructXmlAtt = new Attachment("结构化数据文件", "xml", Files.newInputStream(ofdStructXmlPath));
        ArrayList<Attachment> arrayList = Lists.newArrayList(guaranteeOfdAtt, guaranteeOfdAttPdf, applyFileAtt, applyFileAttPdf,
                qualificationAtt, licenseAtt, receiveOfdAtt, ofdStructXmlAtt);


        Path srcPath = Paths.get("E:\\Workspaces\\ideawork\\maihe\\fzdx\\ningde\\fzdx-web-ningde\\fzdx-manage-api-ningde\\src\\main\\resources\\ofd\\C00061451444672820465665\\unSeal担保保函.ofd");
//        Path srcPath = basePath.resolve("担保保函.ofd");
        Path targetPath = basePath.resolve("担保保函attach.ofd");

        ktConvert.kt_addAttachment(srcPath.toFile(), Files.newOutputStream(targetPath), arrayList);
        System.out.println("convert success!!!");
    }

    public static void main(String[] args) throws IOException, OESException {
//		SealDemoTest();
        //UnionSealDemoTest();
//		ConvertTest();
//        ConvertAddAttachment();
//		ConvertAddAttachments();
    }
}
